package com.qupai.controller.admin;

import com.qupai.entity.PtActivity;
import com.qupai.result.PageResult;
import com.qupai.result.Result;
import com.qupai.dto.*;
import com.qupai.service.PtActivityService;
import com.qupai.vo.PtActivityVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springdoc.core.annotations.ParameterObject;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * 活动管理
 */
@RestController
@RequestMapping("/admin/ptactivities")
@Tag(name = "普通-活动相关接口")
@Slf4j
@RequiredArgsConstructor
public class PtActivityController {

    private final PtActivityService ptActivitiesService;
    private final RedisTemplate<String, Object> redisTemplate;

    @PostMapping
    @Operation(summary = "新增活动")
    public Result<String> save(@RequestBody PtActivityDTO activitiesDTO){
        log.info("新增活动:{}",activitiesDTO);
        ptActivitiesService.save(activitiesDTO);

        //清理缓存数据
        String key = "ptactivities_" + activitiesDTO.getCollegesId();
        cleanCache(key);
        return Result.success();
    }

    /**
     * 活动分页查询
     */
    @GetMapping("/page")
    @Operation(summary = "活动分页查询")
    public Result<PageResult> page(@ParameterObject PtActivityPageQueryDTO activitiesPageQueryDTO){
        log.info("活动分页查询:{}",activitiesPageQueryDTO);
        PageResult pageResult=ptActivitiesService.pageQuery(activitiesPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 删除活动
     */
    @DeleteMapping
    @Operation(summary = "删除活动")
    public Result<String> delete(@RequestParam List<Long> ids){
        log.info("删除活动：{}", ids);
        ptActivitiesService.deleteBatch(ids);

        //将所有的活动数据缓存数据清理掉，所有以activities_开头的key
        cleanCache("ptactivities_*");
        return Result.success();
    }

    /**
     * 根据id查询活动
     */
    @GetMapping("/{id}")
    @Operation(summary = "根据id查询活动")
    public Result<PtActivityVO>getById (@PathVariable Long id){
        log.info("根据id查询活动:{}",id);
        PtActivityVO activitiesVO=ptActivitiesService.getById(id);
        return Result.success(activitiesVO);
    }

    /**
     *  修改活动
     */
    @PutMapping
    @Operation(summary = "修改活动")
    public Result<String> update(@RequestBody PtActivityDTO activitiesDTO){
        log.info("修改活动:{}",activitiesDTO);
        ptActivitiesService.update(activitiesDTO);

        //将所有的活动数据缓存数据清理掉，所有以activities_开头的key
        cleanCache("ptactivities_*");
        return Result.success();
    }

    /**
     *  启用/禁用活动状态
     */
    @PostMapping("/status/{status}")
    @Operation(summary = "启用/禁用活动状态")
    public Result<String> startOrStop(@PathVariable Integer status, Long id){
        log.info("启用/禁用活动状态：{}，{}",status,id);
        //调用service
        ptActivitiesService.startOrStop(status,id);

        //将所有的活动数据缓存数据清理掉，所有以activities_开头的key
        cleanCache("ptactivities_*");
        return Result.success();
    }

    /**
     * 根据各分类id查询活动
     */
    @GetMapping("/list")
    @Operation(summary = "根据各分类id查询活动")
    public Result<List<PtActivityVO>> list(PtActivity activities){
        List<PtActivityVO> list = ptActivitiesService.list(activities);
        log.info("根据各分类id查询活动：{}",list);
        return Result.success(list);
    }

    @PostMapping("/givecoin/{id}")
    @Operation(summary = "给予活动积分")
    private Result<String> givecoin(@PathVariable Long id){
        log.info("发布活动：{} 的积分",id);
        ptActivitiesService.givecoin(id);
        return Result.success();
    }

    private void cleanCache(String pattern){
        Set<String> keys = redisTemplate.keys(pattern);
        if (keys != null && !keys.isEmpty()) {
            redisTemplate.delete(keys);
            log.info("清除缓存成功：{}", keys);
        }else{
            log.info("清除缓存失败：{}", keys);
        }
    }

}
